// 创建上下文菜单
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: 'translateSelection',
    title: '翻译选中文本',
    contexts: ['selection']
  });
});

// 处理上下文菜单点击
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
  if (info.menuItemId === 'translateSelection') {
    // 获取API Key
    const { deepseekApiKey } = await chrome.storage.sync.get('deepseekApiKey');
    if (!deepseekApiKey) {
      // 如果没有API Key，通知用户
      chrome.tabs.sendMessage(tab.id, {
        action: 'showMessage',
        message: '请先在扩展设置中设置 DeepSeek API Key'
      });
      return;
    }

    // 获取显示原文的设置
    const { showOriginal } = await chrome.storage.sync.get('showOriginal');

    // 发送消息到content script进行翻译
    chrome.tabs.sendMessage(tab.id, {
      action: 'translateSelection',
      text: info.selectionText,
      apiKey: deepseekApiKey,
      showOriginal: showOriginal !== undefined ? showOriginal : true
    });
  }
}); 